package com.hzlx.service;

import com.hzlx.bean.Product;
import com.hzlx.dao.ProductDao;
import com.hzlx.dto.ProductRequest;
import com.hzlx.dto.ProductResponse;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductService {
    @Resource ProductDao productDao;
    public void create(ProductRequest request){
        Product product = Product.builder()
                .name(request.getName())
                .description(request.getDescription())
                .price(request.getPrice())
                .sku(request.getSku())
                .build();
        productDao.save(product);
    }
    public List<ProductResponse> findAll(){
        return productDao.findAll().stream()
                .map(product -> ProductResponse.builder()
                        .id(product.getId())
                        .name(product.getName())
                        .description(product.getDescription())
                        .price(product.getPrice())
                        .sku(product.getSku())
                        .build())
                .collect(Collectors.toList());
    }
}
